package com.primecodegen.common.DmAttrProp;

import com.primecodegen.common.AttributesProperties.AttributesPropertiesDAO;
import com.primecodegen.common.AttributesProperties.AttributesPropertiesTable;
import com.primecodegen.common.DataModelAttributes.DataModelAttributesDAO;
import com.primecodegen.common.DataModelAttributes.DataModelAttributesTable;
import com.primecodegen.util.*;
import com.primecodegen.common.base.BaseBean;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.logging.*;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import javax.servlet.http.HttpSession;

@ManagedBean(name = "dmAttrPropBean")
@RequestScoped
public class DmAttrPropBean extends BaseBean implements Serializable {

    // *********** Local Variables/Objects ***********
    private static final Logger logger = Logger.getLogger(DmAttrPropBean.class.getName());
    private int count;
    private int dmAttrPropID;
    private String excludedClmn;
    private DmAttrPropTable selectedDmAttrProp;
    HttpSession session;
    private DmAttrPropDAO dmAttrPropDAO;
    private ArrayList<DmAttrPropTable> dmAttrPropList;
    private DmAttrPropTable dmAttrPropSrchCrit;
    private DmAttrPropTable dmAttrPropUpdate;
    private DmAttrPropTable dmAttrPropTable;
    FacesContext context;
    ColToOrderBy colToOrderBy;

    // *********** Constructors ***********
    public DmAttrPropBean() {
        logger.info(" Constructor DmAttrPropBean()");
        excludedClmn = "6";
        context = getContext();
        session = getSession();
        dmAttrPropDAO = new DmAttrPropDAO();
        dmAttrPropList = new ArrayList<DmAttrPropTable>();
        dmAttrPropTable = new DmAttrPropTable();
        dmAttrPropSrchCrit = (DmAttrPropTable) session.getAttribute("sessionDmAttrPropSrchCrit");
        dmAttrPropUpdate = (DmAttrPropTable) session.getAttribute("dmAttrPropUpdateSess");
        colToOrderBy = (ColToOrderBy) session.getAttribute("sessionColToOrderBy");

        if (dmAttrPropSrchCrit == null) {
            dmAttrPropSrchCrit = new DmAttrPropTable();
        }

        if (dmAttrPropUpdate == null) {
            dmAttrPropUpdate = new DmAttrPropTable();
        }



        fillDmAttrPropList();
    }

    // *********** Setters & Getters ***********
    public String getExcludedClmn() {
        return excludedClmn;
    }

    public void setExcludedClmn(String excludedClmn) {
        this.excludedClmn = excludedClmn;
    }

    public DmAttrPropTable getDmAttrPropUpdate() {
        dmAttrPropUpdate = (DmAttrPropTable) session.getAttribute("dmAttrPropUpdateSess");
        if (dmAttrPropUpdate == null) {
            dmAttrPropUpdate = new DmAttrPropTable();
        }
        return dmAttrPropUpdate;
    }

    public void setDmAttrPropUpdate(DmAttrPropTable dmAttrPropUpdate) {
        session.setAttribute("dmAttrPropUpdateSess", dmAttrPropUpdate);
        this.dmAttrPropUpdate = dmAttrPropUpdate;
    }

    public DmAttrPropBean(DmAttrPropTable dmAttrPropTable) {
        logger.info("Constructor DmAttrPropBean( DmAttrPropTable dmAttrPropTable )");
        this.dmAttrPropTable = dmAttrPropTable;
    }

    public DmAttrPropTable getDmAttrPropTable() {
        return dmAttrPropTable;
    }

    public void setDmAttrPropTable(DmAttrPropTable dmAttrPropTable) {
        this.dmAttrPropTable = dmAttrPropTable;
    }

    public DmAttrPropTable getDmAttrPropSrchCrit() {
        return dmAttrPropSrchCrit;
    }

    public void setDmAttrPropSrchCrit(DmAttrPropTable dmAttrPropSrchCrit) {
        this.dmAttrPropSrchCrit = dmAttrPropSrchCrit;
    }

    public DmAttrPropTable getSelectedDmAttrProp() {
        return selectedDmAttrProp;

    }

    public void setSelectedDmAttrProp(DmAttrPropTable selectedDmAttrProp) {
        this.selectedDmAttrProp = selectedDmAttrProp;
    }

    public ArrayList<DmAttrPropTable> getDmAttrPropList() {
        return dmAttrPropList;
    }
//******************************
    private ArrayList<SelectItem> dataModelAttributesList = new ArrayList<SelectItem>();
    private DataModelAttributesDAO dataModelAttributesDAO = new DataModelAttributesDAO();

    public ArrayList<SelectItem> getDataModelAttributesList() {
        getAllDataModelAttributes();
        return dataModelAttributesList;
    }

    public void setDataModelAttributesList(ArrayList<SelectItem> dataModelAttributesList) {
        this.dataModelAttributesList = dataModelAttributesList;
    }

    public void getAllDataModelAttributes() {
        dataModelAttributesList = new ArrayList<SelectItem>();
        DataModelAttributesTable groupTable = new DataModelAttributesTable();
        dataModelAttributesList.add(new SelectItem("", "- Select Role -"));
        try {
            Iterator itDataModelAttributes = dataModelAttributesDAO.getByCriterionOrderedBy(null, null).iterator();
            while (itDataModelAttributes.hasNext()) {
                groupTable = (DataModelAttributesTable) itDataModelAttributes.next();
                dataModelAttributesList.add(new SelectItem(groupTable.getID(), groupTable.getCode()));
                groupTable = new DataModelAttributesTable();
            }

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in public void getAllDataModelAttributes() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }
    //******************************

    


   // ***************************
    private ArrayList<SelectItem> attributesPropertiesList = new ArrayList<SelectItem>();
    private AttributesPropertiesDAO attributesPropertiesDAO = new AttributesPropertiesDAO();

    public ArrayList<SelectItem> getAttributesPropertiesList() {
        getAllAttributesProperties();
        return attributesPropertiesList;
    }

    public void setAttributesPropertiesList(ArrayList<SelectItem> attributesPropertiesList) {
        this.attributesPropertiesList = attributesPropertiesList;
    }

    public void getAllAttributesProperties() {
        attributesPropertiesList = new ArrayList<SelectItem>();
        AttributesPropertiesTable groupTable = new AttributesPropertiesTable();
        attributesPropertiesList.add(new SelectItem("", "- Select Role -"));
        try {
            Iterator itAttributesProperties = attributesPropertiesDAO.getByCriterionOrderedBy(null, null).iterator();
            while (itAttributesProperties.hasNext()) {
                groupTable = (AttributesPropertiesTable) itAttributesProperties.next();
                attributesPropertiesList.add(new SelectItem(groupTable.getID(), groupTable.getCode()));
                groupTable = new AttributesPropertiesTable();
            }

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in public void getAllAttributesProperties() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }
    //**

    // *********** Actions ***********
    public void doFilter() {
        logger.log(Level.INFO, "Starting: public void doFilter () in DmAttrPropBean");
        try {
            dmAttrPropSrchCrit.setFilter(true);
            this.dmAttrPropList = dmAttrPropDAO.getByCriterionOrderedBy(dmAttrPropSrchCrit, null);

            session.setAttribute("sessionDmAttrPropSrchCrit", dmAttrPropSrchCrit);
            fillDmAttrPropList();
            logger.log(Level.INFO, "Finishing: public void doFilter () in DmAttrPropBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in doFilter() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }

    public void clearFilter() {
        try {
            dmAttrPropSrchCrit = null;
            session.setAttribute("sessionDmAttrPropSrchCrit", dmAttrPropSrchCrit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sortByID() {

        logger.log(Level.INFO, "public void fillDmAttrPropList ()");

        try {

            if (dmAttrPropSrchCrit != null && colToOrderBy != null) {
                this.dmAttrPropList = dmAttrPropDAO.getByCriterionOrderedBy(dmAttrPropSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dmAttrPropList = dmAttrPropDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dmAttrPropSrchCrit != null) {
                this.dmAttrPropList = dmAttrPropDAO.getByCriterionOrderedBy(dmAttrPropSrchCrit, null);
            } else {
                this.dmAttrPropList = dmAttrPropDAO.getByCriterionOrderedBy(null, null);
            }




        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDmAttrPropList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }

    public void update() {
        try {
            int parID = new Integer(session.getAttribute("dmAttrPropIDSess").toString());
            dmAttrPropUpdate.setID(parID);
            dmAttrPropDAO.updateByID(dmAttrPropUpdate);

        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured interface the update action, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in Update(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void Insert() {
        logger.info("public void Insert()");
        try {

            int isInserted = -1;


            isInserted = dmAttrPropDAO.insert(dmAttrPropTable);

            if (isInserted != -1) {
                getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved Successfully", "DmAttrProp Added"));
                fillDmAttrPropList();
            } else {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "error", "error shit"));
                logger.log(Level.WARNING, "Exception occured in Insert(). Error Message: isInserted != -1 Probably exception occure in dmAttrPropDAO.insert( dmAttrPropTable )");

            }

        } catch (Exception e) {

            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void delete() {
        try {

            int parID = new Integer(session.getAttribute("dmAttrPropIDSess").toString());



            dmAttrPropDAO.deleteByID(parID);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "DmAttrProp Deleted"));
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure", "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
        }
    }

    public void setIDinSession() {
        try {
            Map<String, String> params = context.getExternalContext().getRequestParameterMap();
            int parID = new Integer(params.get("dmAttrPropID"));
            dmAttrPropUpdate = (DmAttrPropTable) dmAttrPropDAO.getByID(parID);
            session.setAttribute("dmAttrPropUpdateSess", dmAttrPropUpdate);
            session.setAttribute("dmAttrPropIDSess", parID);
        } catch (Exception ex) {
            Logger.getLogger(DmAttrPropBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fillDmAttrPropList() {
        logger.log(Level.INFO, "Starting: public void fillDmAttrPropList () in DmAttrPropBean");
        try {

            if ((dmAttrPropSrchCrit != null && dmAttrPropSrchCrit.isFilter()) && colToOrderBy != null) {
                this.dmAttrPropList = dmAttrPropDAO.getByCriterionOrderedBy(dmAttrPropSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dmAttrPropList = dmAttrPropDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dmAttrPropSrchCrit != null && dmAttrPropSrchCrit.isFilter()) {
                this.dmAttrPropList = dmAttrPropDAO.getByCriterionOrderedBy(dmAttrPropSrchCrit, null);
            } else {
                this.dmAttrPropList = dmAttrPropDAO.getByCriterionOrderedBy(null, null);
            }



            logger.log(Level.INFO, "finishing: public void fillDmAttrPropList () in DmAttrPropBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDmAttrPropList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }
}
